<template>
  <div id="detail">
    <detail-nav-bar @itemClick="titleClick" :current-index="currentIndex"></detail-nav-bar>
    <scroll class="content"
            ref="scroll"
            @scroll="contentScroll"
            :data="[topImages, goods, shop, detailInfo, paramInfo, commentInfo, recommendList]"
            :probe-type="3"> 
      <div>
        <detail-swiper ref="base" :images="topImages"></detail-swiper>
        <detail-base-info :goods="goods"></detail-base-info>
        <detail-shop-info :shop="shop"></detail-shop-info>
        <detail-goods-info :detail-info="detailInfo"></detail-goods-info>
        <detail-param-info ref="param" :param-info="paramInfo"></detail-param-info>
        <detail-comment-info ref="comment" :comment-info="commentInfo"></detail-comment-info>
        <detail-recommend-info ref="recommend" :recommend-list="recommendList"></detail-recommend-info>
      </div>
    </scroll>
    <detail-bottom-bar @addToCart="addToCart"  @addToCart2="addToCart2" @addToCart3="addToCart3"></detail-bottom-bar>
    <!-- <detail-bottom-bar @addToCart2="addToCart2"></detail-bottom-bar> -->

    <back-top @backTop="backTop" class="back-top" v-show="showBackTop">
      <img src="~assets/img/common/top.png" alt="">
    </back-top>
    <toast :message="message" :show="show"/>
  </div>
</template>

<script>
  import Scroll from 'common/scroll/Scroll'

  import DetailNavBar from './childComps/DetailNavBar'
  import DetailSwiper from './childComps/DetailSwiper'
  import DetailBaseInfo from './childComps/DetailBaseInfo'
  import DetailShopInfo from './childComps/DetailShopInfo'
  import DetailGoodsInfo from './childComps/DetailGoodsInfo'
  import DetailParamInfo from './childComps/DetailParamInfo'
  import DetailCommentInfo from './childComps/DetailCommentInfo'
  import DetailRecommendInfo from './childComps/DetailRecommendInfo'

  import DetailBottomBar from './childComps/DetailBottomBar'
  import BackTop from 'content/backTop/BackTop'

import Toast from 'components/common/toast/Toast'


  import {getDetail, getRecommend, Goods, Shop, GoodsParam} from "network/detail";
  import {backTopMixin} from "@/common/mixin";
  import {BACKTOP_DISTANCE} from "@/common/const";

  export default {
		name: "Detail",
    components: {
      DetailBaseInfo,
      DetailShopInfo,
      DetailGoodsInfo,
      DetailParamInfo,
      DetailCommentInfo,
      DetailRecommendInfo,
      DetailBottomBar,
		  Scroll,
		  DetailNavBar,
      DetailSwiper,
      BackTop,
      Toast
    },
    mixins: [backTopMixin],
    data() {
		  return {
		    iid: '',
        topImages: ["//img.alicdn.com/imgextra/i1/111429238/TB258ybb6QnBKNjSZSgXXXHGXXa_!!0-saturn_solar.jpg_220x220.jpg_.webp", "//img.alicdn.com/imgextra/i1/111429238/TB258ybb6QnBKNjSZSgXXXHGXXa_!!0-saturn_solar.jpg_220x220.jpg_.webp","//img.alicdn.com/imgextra/i1/111429238/TB258ybb6QnBKNjSZSgXXXHGXXa_!!0-saturn_solar.jpg_220x220.jpg_.webp","//img.alicdn.com/imgextra/i1/111429238/TB258ybb6QnBKNjSZSgXXXHGXXa_!!0-saturn_solar.jpg_220x220.jpg_.webp"], 
        
        goods:  { title:'',explain:'',price:0,id:0,oldPrice:1000,discount:'优惠价',columns:{0:'销量：12455',1:'收藏：6000',2:'12小时发货'}, services:[{icon:'https://s11.mogucdn.com/mlcdn/c45406/180417_25kbfg1c3hdbd120394ji4b11bk2k_36x36.png',name:'退货补运费'},{icon:'https://s11.mogucdn.com/mlcdn/c45406/180417_25kbfg1c3hdbd120394ji4b11bk2k_36x36.png',name:'全国包邮'},{icon:'https://s11.mogucdn.com/mlcdn/c45406/180417_25kbfg1c3hdbd120394ji4b11bk2k_36x36.png',name:'7天无理由退货'}] },

        shop:  {name:'波波波网',logo:'//gaitaobao1.alicdn.com/tfscom/i4/59873468/O1CN01fyZykk1bUO4F1ojeO_!!59873468.jpg_240x240xz.jpg_.webp',sells:40555,goodsCount:45,score:3, }, 

        detailInfo:{ detailImage: '//gaitaobao1.alicdn.com/tfscom/i4/59873468/O1CN01fyZykk1bUO4F1ojeO_!!59873468.jpg_240x240xz.jpg_.webp',desc:'新款',detailImage:[{ list:{0:'https://s5.mogucdn.com/mlcdn/c45406/190603_5f8li8k06b58f9e60j5l3851b7656_640x960.jpg_468x468.jpg',  1:'https://s5.mogucdn.com/mlcdn/c45406/190603_5f8li8k06b58f9e60j5l3851b7656_640x960.jpg_468x468.jpg',2:'https://s5.mogucdn.com/mlcdn/c45406/190603_5f8li8k06b58f9e60j5l3851b7656_640x960.jpg_468x468.jpg',3:'https://s5.mogucdn.com/mlcdn/c45406/190603_5f8li8k06b58f9e60j5l3851b7656_640x960.jpg_468x468.jpg',5:'https://s5.mogucdn.com/mlcdn/c45406/200402_6k57cjaai908dj3fbhk8h1k641j1e_2000x3000.jpg_640x854.v1cAC.40.webp',6:'//gaitaobao1.alicdn.com/tfscom/i4/59873468/O1CN01fyZykk1bUO4F1ojeO_!!59873468.jpg_240x240xz.jpg_.webp'} ,key:'穿着效果',   }, ]},

        paramInfo: {sizes:4, infos:5,   value:'广州白云集团',key:'厂址',image: '//img.alicdn.com/imgextra/i1/111429238/TB258ybb6QnBKNjSZSgXXXHGXXa_!!0-saturn_solar.jpg_220x220.jpg_.webp',},
        
        commentInfo: {user: {avatar:'https://s5.mogucdn.com/mlcdn/c45406/190603_5f8li8k06b58f9e60j5l3851b7656_640x960.jpg_468x468.jpg', uname:' 穿上它你就是整条街最靓的仔。', }, content:'靓仔靓仔，你好',created:8888 ,style:'鱼在大海里',images:3},   

        recommendList: { },

        themeTops: [],
        currentIndex: 0,
        message:'',
        show:false
      }
    },
    created() {
		  this._getDetailData()
      this._getRecommend()
    },
    updated() {
		  // 获取需要的四个offsetTop
      this._getOffsetTops()
    },
    methods: {
		  _getOffsetTops() {
		    this.themeTops = []
        this.themeTops.push(this.$refs.base.$el.offsetTop)
        this.themeTops.push(this.$refs.param.$el.offsetTop)
        this.themeTops.push(this.$refs.comment.$el.offsetTop)
        this.themeTops.push(this.$refs.recommend.$el.offsetTop)
        this.themeTops.push(Number.MAX_VALUE)
      },
      contentScroll(position) {
		    // 1.监听backTop的显示
        this.showBackTop = position.y < -BACKTOP_DISTANCE

        // 2.监听滚动到哪一个主题
        this._listenScrollTheme(-position.y)
      },
      _listenScrollTheme(position) {
        let length = this.themeTops.length;
        for (let i = 0; i < length; i++) {
          let iPos = this.themeTops[i];
          
          if (position >= iPos && position < this.themeTops[i+1]) {
            if (this.currentIndex !== i) {
              this.currentIndex = i;
            }
            break;
          }
        }
      },
      titleClick(index) {
        // console.log(this.themeTops[index]);
        this.$refs.scroll.scrollTo(0, -this.themeTops[index], 100)
      },
  
      addToCart() {
        // 1.创建对象
        const obj = {}
        // 2.对象信息
        obj.iid = this.iid;
        obj.imgURL = this.topImages[0] 
        obj.title = this.goods.title
        obj.desc = this.goods.desc;
        obj.price = this.goods.price;  
        obj.id = this.goods.id;  
        obj.desc = this.goods.desc;
        obj.explain=this.goods.explain;
        // 3.添加到Store中
        this.$store.commit('addCart', obj)
       
       this.show =true;
       this.message='添加购物车成功';
       setTimeout(() => {
         this.show=false;
           this.message=''
       },1500)
      },
       addToCart2() {
        // 1.创建对象
        const objj = {}
        // 2.对象信息
        objj.iid = this.iid;
        objj.imgURL = this.topImages[0] 
        objj.title = this.goods.title
        objj.desc = this.goods.desc;
        objj.price = this.goods.price;  
        objj.id = this.goods.id;  
        objj.desc = this.goods.desc;
        objj.explain=this.goods.explain;
        // 3.添加到Store中
        this.$store.commit('addCart2', objj)
       
       this.show =true;
       this.message='添加收藏';
       setTimeout(() => {
         this.show=false;
           this.message=''
       },1500)
      },   
      addToCart3() {
        this.$store.commit('addCart3')
       
       this.show =true;
       this.message='合并';
       setTimeout(() => {
         this.show=false;
           this.message=''
       },1500)
      },
		  _getDetailData() {
		    // 1.获取iid
        const iid = this.$route.query.iid
        this.iid = iid;
         
          this.goods.title =this.$route.query.title;
          this.goods.price = this.$route.query.price;
          this.goods.id = this.$route.query.id;
          this.goods.desc=this.$route.query.desc;
          this.goods.explain=this.$route.query.explain;
      },
      _getRecommend() {
        // getRecommend().then((res, error) => {
        //   if (error) return
        //   this.recommendList = res.data.list
        // })
      }
    }
	}
</script>

<style scoped>
  #detail {
    height: 100vh;
    position: relative;
    z-index: 1;
    background-color: #fff;
  }

  .content {
    position: absolute;
    top: 44px;
    bottom: 60px;
  }

  .back-top {
    position: fixed;
    right: 10px;
    bottom: 65px;
  }
</style>
